/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdcache.distributed.server;

//import com.pyro.logging.LogManager;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author chaitanya
 */
public class ServerSocketInterface extends Thread
{

    private ServerSocket serverSocket;
    private ServerEventListener listener;
    private int port;

    public ServerSocketInterface(String name,int port,ServerEventListener listener)
    {
        super(name+" Server");
        this.port=port;
        this.listener=listener;
    //    log=new LogManager();
    }
    


    @Override
    public void run()
    {
        while(true)
        {
            try
            {
                serverSocket= new ServerSocket(port);
                break;
            } catch (Exception exception)
            {
                exception.printStackTrace();
            }
            try{
                Thread.sleep(5000);
            }catch(Exception ex){}
        }
        while (true)
        {
            try
            {
                Socket sock = serverSocket.accept();
                ClientHandler handler = new ClientHandler(getName(),sock,4);
                Processor processor = listener.newClient(handler);
                handler.startProcessing(processor);
            } catch (Exception e)
            {
                System.out.println("Exception e"+e.getMessage());
                e.printStackTrace();
            }
        }
    }

  
    
}
